package com.eliota.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eliota.common.R;
import com.eliota.entity.Maintain;
import com.eliota.entity.Repair;
import com.eliota.service.MaintainService;
import com.eliota.service.RepairService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author MG
 * @since 2023-04-03
 */
@RestController
@RequestMapping("/repair")
public class RepairController {

    @Resource
    private RepairService repairService;

    @Resource
    private MaintainService maintainService;


    /**
     * 插入
     * @param repair
     * @return
     */
    @PostMapping("/save")
    public R save(@RequestBody Repair repair){
        return R.success(repairService.saveOrUpdate(repair));
    }


    /**
     *查询条件：deviceName：维修设备名
     *
     *
     * @param pageNum
     * @param pageSize
     * @param repairmanName 维修维修工姓名
     *
     * repairId          排序规则：降序
     * @return
     */
    @GetMapping("/page")
    public R findPage(@RequestParam Integer pageNum,
                      @RequestParam Integer pageSize,
                      @RequestParam String repairmanName){
        Page<Repair> repairPage = new Page<>(pageNum, pageSize);
        QueryWrapper<Repair> repairQueryWrapper= new QueryWrapper<>();
        repairQueryWrapper.like("repairman_name",repairmanName).eq("status",0).orderByDesc("repair_id");

        return R.success(repairService.page(repairPage,repairQueryWrapper));
    }


    @GetMapping("/{repairId}")
    public R findByUserName(@PathVariable Integer repairId){
        Repair getOneById = repairService.getOne(new QueryWrapper<Repair>().eq("repair_id", repairId));
        return R.success(getOneById);
    }



    @DeleteMapping("/{repairId}")
    public R delete(@PathVariable Integer repairId){
        repairService.statusChange(repairId);
        return R.success();
    }

    @PostMapping("/bath/del")
    public R deleteBath(@RequestBody List<Integer> ids){
        repairService.deleteBatchByIds(ids);
        return R.success();
    }


    @GetMapping("/list")
    public R findMaintain(){
        return R.success(maintainService.list());
    }


}
